package cn.stylefeng.guns.modular.business.frequency.transportsignimage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.transportsignimage.entity.TransportSignImage;
import cn.stylefeng.guns.modular.business.frequency.transportsignimage.mapper.TransportSignImageMapper;
import cn.stylefeng.guns.modular.business.frequency.transportsignimage.model.param.TransportSignImageParam;
import cn.stylefeng.guns.modular.business.frequency.transportsignimage.service.TransportSignImageService;
import cn.stylefeng.guns.modular.business.frequency.transportsignimage.enums.TransportSignImageExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运单签收图片 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:15
 */
@Service
public class TransportSignImageServiceImpl extends ServiceImpl<TransportSignImageMapper, TransportSignImage> implements TransportSignImageService {

    @Override
    public PageResult<TransportSignImage> page(TransportSignImageParam transportSignImageParam) {

        // 构造条件
        LambdaQueryWrapper<TransportSignImage> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<TransportSignImage> list(TransportSignImageParam transportSignImageParam) {

        // 构造条件
        LambdaQueryWrapper<TransportSignImage> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(TransportSignImageParam transportSignImageParam) {

        // 将dto转为实体
        TransportSignImage transportSignImage = new TransportSignImage();
        BeanUtil.copyProperties(transportSignImageParam, transportSignImage);

        this.save(transportSignImage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(TransportSignImageParam transportSignImageParam) {
        this.removeById(transportSignImageParam.getId());
    }

    @Override
    public void edit(TransportSignImageParam transportSignImageParam) {

        // 根据id查询实体
        TransportSignImage transportSignImage = this.queryTransportSignImage(transportSignImageParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(transportSignImageParam, transportSignImage);

        this.updateById(transportSignImage);
    }

    @Override
    public TransportSignImage detail(TransportSignImageParam transportSignImageParam) {
        return this.queryTransportSignImage(transportSignImageParam);
    }



    /**
     * 获取运单签收图片
     *
     * @author yujie
     * @since 2023/05/09 17:15
     */
    private TransportSignImage queryTransportSignImage(TransportSignImageParam transportSignImageParam) {
        TransportSignImage transportSignImage = this.getById(transportSignImageParam.getId());
        if (ObjectUtil.isEmpty(transportSignImage)) {
            throw new ServiceException(TransportSignImageExceptionEnum.NOT_EXISTED);
        }
        return transportSignImage;
    }
}